package com.yifeng.repo.base.mdm.full.holder;

import com.yifeng.base.bds.api.store.dto.StoreCorporationRegionInfoDto;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

/**
 * Created by daibing on 2024/6/18.
 */
public class StoreRouteHolder {
    private static final ConcurrentMap<String, StoreCorporationRegionInfoDto> STORE_CODE_2_ROUTE = new ConcurrentHashMap<>();

    public static int count() {
        return STORE_CODE_2_ROUTE.size();
    }

    public static StoreCorporationRegionInfoDto get(String storeCode) {
        return STORE_CODE_2_ROUTE.get(storeCode);
    }

    public static List<StoreCorporationRegionInfoDto> list(Collection<String> storeCodes) {
        return storeCodes.stream().map(StoreRouteHolder::get).collect(Collectors.toList());
    }

    public static void add(StoreCorporationRegionInfoDto storeRouteDto) {
        STORE_CODE_2_ROUTE.put(storeRouteDto.getStoreCode(), storeRouteDto);
    }

    public static void add(Collection<StoreCorporationRegionInfoDto> storeRouteDtos) {
        Map<String, StoreCorporationRegionInfoDto> collect = storeRouteDtos.stream()
                .collect(Collectors.toMap(StoreCorporationRegionInfoDto::getStoreCode, t -> t));
        STORE_CODE_2_ROUTE.putAll(collect);
    }

    public static void remove(String storeCode) {
        STORE_CODE_2_ROUTE.remove(storeCode);
    }

    public static void clear() {
        STORE_CODE_2_ROUTE.clear();
    }

}
